package com.macro.service.impl;


import com.macro.enums.AppHttpCodeEnum;
import com.macro.mapper.RoleMapper;
import com.macro.pojos.dto.ResponseResult;
import com.macro.pojos.dto.RoleDto;
import com.macro.pojos.pojo.Role;
import com.macro.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> findAllRole() {
        List<Role> roles = roleMapper.selectList(null);
        return roles;
    }

    @Override
    public ResponseResult AddRole(RoleDto roleDto) {
        if (roleDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Role role = new Role();
        role.setName(roleDto.getName());
        role.setKeyword(roleDto.getKeyword());
        role.setContext(roleDto.getContext());
        role.setCreateTime(new Date());
        role.setIsDelete(false);
        roleMapper.insert(role);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateRoleDto(RoleDto roleDto) {
        if (roleDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Role role = roleMapper.selectById(roleDto.getId());
        if (role==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        role.setName(roleDto.getName());
        role.setKeyword(roleDto.getKeyword());
        role.setContext(roleDto.getContext());
        roleMapper.updateById(role);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delRole(Integer id) {
        if (id==null){
            return ResponseResult.errorResult(400,"id为空");
        }
        Role role = roleMapper.selectById(id);
        if (role==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        role.setIsDelete(true);
        roleMapper.updateById(role);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
